package com.yuanfeng.userms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import com.yuanfeng.userms.dto.AdminDepartmentDTO;
import com.yuanfeng.userms.entity.UserAdminDepartmentEntity;
import com.yuanfeng.userms.mapper.UserAdminDepartmentMapper;
import com.yuanfeng.userms.service.UserAdminDepartmentService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class UserAdminDepartmentServiceImpl extends ServiceImpl<UserAdminDepartmentMapper, UserAdminDepartmentEntity>
    implements UserAdminDepartmentService{

    @Override
    public ResponseResult deleteDepartment(AdminDepartmentDTO adminDepartmentDto) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(adminDepartmentDto.getToken());
        if (null != userInfo) {
            //判断当前部门是否有使用
            Integer result = this.baseMapper.selectDepartmentIsUser(adminDepartmentDto.getId());
            if (result > 0) {
                return ResponseResult.fail(BizCodeEnume.DELETE_ADMIN_DEPARTMENT_USER_ERROR);
            }
            //删除部门
            this.baseMapper.deleteById(adminDepartmentDto.getId());
            return ResponseResult.success();
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }

    @Override
    public ResponseResult queryDepartmentAll(Map<String, Object> map) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(map.get("token").toString());
        if (null != userInfo) {
            List<UserAdminDepartmentEntity> list = this.baseMapper.selectList(new QueryWrapper<>());
            return ResponseResult.success(list);
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }

    @Override
    public ResponseResult addDepartment(AdminDepartmentDTO adminDepartmentDto) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(adminDepartmentDto.getToken());
        if (null != userInfo) {
            //新增或修改部门
            this.baseMapper.addDepartment(adminDepartmentDto);
            return ResponseResult.success();
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }
}




